#ifndef _DEFINES_H_
#define _DEFINES_H_
#define BIT_MASK ((long long)1)

#define MAX_NAME 255
#define BUF_SIZE 1023
#define READ_SIZE 1
#define HASH_LENGTH 32
#define IP_LENGTH 15
#define PORT_LENGTH 5
#define IPPORT_LENGTH (IP_LENGTH + PORT_LENGTH + 1)
#define NUM_MAX_PEERS 3
#define MAX_BMSIZE 1000000

#define PIECE_SIZE 2048
#define SHARING_PATH "./shared"

int readi;
#define READ(fd, buff, ready, readc, offset) \
 \
  erase(buff, BUF_SIZE); \
 \
  while(1) { \
       \
    readc = read(fd, buff+offset, READ_SIZE); \
 \
    ready=0; \
    for(readi=0; readi<READ_SIZE; readi++) { \
      if(buff[readi+offset]==' ' || buff[readi+offset]=='\n' || buff[readi+offset]=='\r') buff[readi+offset]='\0'; /* Pour telnet */ \
      if(buff[readi+offset]=='\0') ready=1; \
    } \
     \
    if(readc <= 0 && !errno) { \
      perror("Connection to peer lost, exiting"); \
      exit(0); \
    } \
    else \
      offset += readc; \
     \
    if(ready && buff[0]!='\0') { \
        ready = 0; \
        offset = 0; \
        break; \
    } \
  }
  
#define READ_DATA(fd, buff, readc, offset, size) \
 \
  while(offset<size) { \
       \
    readc = read(fd, buff+offset, READ_SIZE); \
     \
    if(readc <= 0 && !errno) { \
      perror("Connection to peer lost, exiting"); \
      exit(0); \
    } \
    else \
      offset += readc; \
  }

#define REMPLIR() \
      struct file *f = malloc(sizeof(*f)); \
      strcpy(f->key, "8905e92afeb80fc7722ec89eb0bf0966"); \
      strcpy(f->name, "pouet8.avi"); \
      f->file_size = 3*8+4; \
      f->piece_size = 1; \
      f->buffermap[0] = '#'; \
      f->buffermap[1] = '#'; \
      f->buffermap[2] = '\0'; \
      f->buffermap[3] = '\0'; \
      f->peers = NULL; \
      hashmap_lfiles = add_file(f, hashmap_lfiles); \
       \
      struct file *f2 = malloc(sizeof(*f2)); \
      strcpy(f2->key, "7905e92afeb80fc7722ec89eb0bf0966"); \
      strcpy(f2->name, "pouet7.avi"); \
      f2->file_size = 77; \
      f2->piece_size = 1; \
      f2->buffermap[0] = '#'; \
      f2->buffermap[1] = '#'; \
      f2->peers = NULL; \
      hashmap_sfiles = add_file(f2, hashmap_sfiles); \
       \
       \
      struct file *f3 = malloc(sizeof(*f3)); \
      strcpy(f3->key, "6905e92afeb80fc7722ec89eb0bf0966"); \
      strcpy(f3->name, "pouet6.avi"); \
      f3->file_size = 66; \
      f3->piece_size = 1; \
      f3->buffermap[0] = '#'; \
      f3->buffermap[1] = '#'; \
      f3->peers = NULL; \
      hashmap_sfiles = add_file(f3, hashmap_sfiles); \
       \
      struct file *f4 = malloc(sizeof(*f4)); \
      strcpy(f4->key, "5905e92afeb80fc7722ec89eb0bf0966"); \
      strcpy(f4->name, "pouet5.avi"); \
      f4->file_size = 55; \
      f4->piece_size = 1; \
      f4->buffermap[0] = 'A'; \
      f4->buffermap[MAX_BMSIZE] = 'A'; \
      f4->peers = NULL; \
      hashmap_lfiles = add_file(f4, hashmap_lfiles);


#endif
